home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1990-1992 Michael Davidson.
- * All rights reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for any purpose and without fee is hereby
- * granted, provided that the above copyright notice appear in all
- * copies and that both that copyright notice and this permission
- * notice appear in supporting documentation.
- *
- * This software is provided "as is" without express or implied warranty.
- */
-
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #if !defined(SVR4)
- #include <sys/keyboard.h>
- #include <sys/console.h>
- #endif
- #include <sys/termio.h>
- #include <sys/signal.h>
- #if defined(SVR4)
- #include <sys/kd.h>
- #else
- #include <sys/vtkd.h>
- #endif
-
- #include "video.h"
-
-
- STATIC struct termio old_tty;
- STATIC struct termio new_tty;
- STATIC char strmap[STRTABLN];
- extern char kbstrmap[];
- STATIC int got_tty;
- STATIC int kbd_fd;
- STATIC int timeout;
-
-
- /*ARGSUSED*/
- STATIC void
- sigalarm(
- int sig
- )
- {
- (void) signal(SIGALRM, sigalarm);
- timeout++;
- }
-
- /*
- * kbdInit() - save initial tty mode
- * save initial function key string table
- * set keyboard into "raw" mode
- * install new function key string table
- * initialize the screen-switch handler
- */
- void
- kbdInit()
- {
- kbd_fd = 0;
-
- if (ioctl(kbd_fd, TCGETA, &old_tty) != 0)
- fatal(errno, "kbdInit() - can't get current tty mode");
-
- if (ioctl(kbd_fd, GIO_STRMAP, strmap) != 0)
- fatal(errno, "kbdInit() - can't get function key string table");
-
- got_tty = 1;
- new_tty = old_tty;
- new_tty.c_iflag &= ~(ISTRIP|INLCR|IGNCR|ICRNL|IUCLC);
- new_tty.c_lflag &= ~(ICANON|ECHO);
- new_tty.c_cc[VMIN] = 1;
- new_tty.c_cc[VTIME] = 0;
-
- if (ioctl(kbd_fd, TCSETAW, &new_tty) != 0)
- fatal(errno, "kbdInit() - can't set new tty mode");
-
- if (ioctl(kbd_fd, PIO_STRMAP, kbstrmap) != 0)
- fatal(errno, "kbdInit() - can't set function key string table");
-
- (void) signal(SIGALRM, sigalarm);
- }
-
- void
- kbdReset()
- {
- if (got_tty)
- {
- got_tty = 0;
-
- if (ioctl(kbd_fd, TCSETAW, &old_tty) != 0)
- fatal(errno, "kbdReset() - can't restore previous tty mode");
-
- if (ioctl(kbd_fd, PIO_STRMAP, strmap) != 0)
- fatal(errno, "kbdReset() - can't restore previous string table");
- }
- }
-
- static int
- kbdRead()
- {
- unsigned char c;
-
- do
- {
- vidCheckScreenSwitch(0);
- } while (read(kbd_fd, &c, 1) != 1 && timeout == 0);
-
- return timeout ? -1 : (int)c;
- }
-
- int
- kbdGetKey(
- int delay
- )
- {
- int k;
- extern short extkeymap[];
-
- if (delay == 0 && rdchk(kbd_fd) == 0)
- {
- vidCheckScreenSwitch(0);
- return 0;
- }
-
- timeout = 0;
- if (delay > 0)
- alarm(delay);
-
- if ((k = kbdRead()) < 0) /* got a timeout */
- return 0;
-
- if (delay > 0)
- alarm(0);
- timeout = 0;
-
- return (k == 0xff) ? extkeymap[kbdRead()] : k;
- }
-
- void
- kbdBell()
- {
-
- ioctl(kbd_fd, KIOCSOUND, 2000);
- nap(200);
- ioctl(kbd_fd, KIOCSOUND, 0);
- }
-